/**
 * jquery.sort.js
 * 商品发布-选择分类
 * author: 锐不可挡
 * date: 2016-07-07
 **/
var productCategoryList = [];

var parentCategory,firstCategory,secondCategory,thirdCategory;
var arrow = " > ";

function initCategory(productCategoryUrl) {
    $.ajax({
        url: productCategoryUrl,
        type: "GET",
        dataType: "JSON",
        async: false,
        success: function (res) {
            productCategoryList = res.data;
            parentCategory = "";
            for (var i = 0; i < productCategoryList.length; i++) {
                if (productCategoryList[i].pid == 0) {
                    parentCategory += '<li categoryId="' + productCategoryList[i].id + '" onClick="selectFirstProductCategory(' + productCategoryList[i].id + ');"><a href="javascript:void(0)">' + productCategoryList[i].name + '</a></li>';
                }
            }
            $("#sort1").html(parentCategory);
        }
    });
}
function initSelectProductCategory(path) {
    var ids = path.split("_");
    var idsLength = ids.length;
    if(idsLength > 1){
        selectFirstProductCategory(ids[1]);
    }
    if(idsLength > 2){
        selectSecondProductCategory(ids[2]);
    }
    if(idsLength > 3){
        selectThirdProductCategory(ids[3]);
    }
}
/*选择一级目录*/
function selectFirstProductCategory(id) {
    parentCategory = "";
    for (var i = 0; i < productCategoryList.length; i++) {
        if (productCategoryList[i].pid == id) {
            parentCategory += '<li categoryId="' + productCategoryList[i].id + '" onClick="selectSecondProductCategory(' + productCategoryList[i].id + ');"><a href="javascript:void(0)">' + productCategoryList[i].name + '</a></li>';
        }
        if(id == productCategoryList[i].id){
            firstCategory = productCategoryList[i];
        }
    }
    $("#sort2").html(parentCategory).show();
    $("#sort3").hide();
    $("#sort1 li[categoryId='" + id + "']").addClass("active").siblings("li").removeClass("active");
    $("#selectedSort").html(firstCategory.name);
    $("#makeProduct").attr("disabled",true);
}

/*选择二级目录*/
function selectSecondProductCategory(id) {
    parentCategory = "";
    for (var i = 0; i < productCategoryList.length; i++) {
        if (productCategoryList[i].pid == id) {
            parentCategory += '<li categoryId="' + productCategoryList[i].id + '" onClick="selectThirdProductCategory(' + productCategoryList[i].id + ');"><a href="javascript:void(0)">' + productCategoryList[i].name + '</a></li>';
        }
        if(id == productCategoryList[i].id){
            secondCategory = productCategoryList[i];
        }
    }
    $("#sort3").html(parentCategory).show();
    $("#sort2 li[categoryId='" + id + "']").addClass("active").siblings("li").removeClass("active");
    $("#selectedSort").html(firstCategory.name + arrow + secondCategory.name);
    $("#makeProduct").attr("disabled",true);
}

/*选择三级目录*/
function selectThirdProductCategory(id) {
    for (var i = 0; i < productCategoryList.length; i++) {
        if(id == productCategoryList[i].id){
            thirdCategory = productCategoryList[i];
        }
    }
    $("#sort3 li[categoryId='" + id + "']").addClass("active").siblings("li").removeClass("active");
    productCategoryName= firstCategory.name + arrow + secondCategory.name + arrow + thirdCategory.name;
    $("#selectedSort").html(productCategoryName);
    $("#makeProduct").removeAttr("disabled");
    productCategoryId = thirdCategory.id;

}

